package dsba.lab3;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.text.html.HTMLWriter;

/**
 * Servlet implementation class webServer
 */
@WebServlet("/webServer")
public class webServer extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public webServer() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		String contenido = "<b>SOY WEB SERVER</b></br>";
		response.getOutputStream().write(contenido.getBytes());
		
//		try{
			URL myURL = new URL("http://localhost:8080/LAB3AppServ/appServ");
			URLConnection myURLConnection = myURL.openConnection();
			myURLConnection.connect();
			BufferedReader reader = new BufferedReader(new InputStreamReader(myURLConnection.getInputStream()));
			StringBuffer sb = new StringBuffer();
			String linea = "";
			while((linea = reader.readLine()) != null){
				sb.append(linea);
			}
			response.getOutputStream().write(sb.toString().getBytes());
//		}
//		catch(MalformedURLException e){			
//		}
		
		
//		response.getOutputStream().write("<b>HOLA MUNDO 2</b>".getBytes());
//		System.out.println("HOLA MUNDO");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
